<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class User_model extends CI_Model
{

    public function login($user, $pass)
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where(array(
            'acc'   =>  $user,
            'pwd'   =>  $pass
        ));
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $return['exists'] = false;
        } else {
            $return['exists'] = true;
            $dataUser = $rs->result_array();
            if ($dataUser[0]['status'] == '0') {
                $return['lockUser'] = true;
            } else {
                $return['lockUser'] = false;
            }
            $return['dataUser'] = $dataUser[0];
        }
        return $return;
    }
    
    public function changePass($user, $pass)
    {
        $this->db->where('id', $user);
        return $this->db->update('users', array(
            'pwd'   =>  $pass
        ));
    }

    
    public function add_user($data) {
        $this->load->database();
        $datas = array(
            'acc' => $data['acc'],
            'pwd' => $data['pwd'],
            'email' => $data['email'],
            'name' => $data['name'],
            'status' => '1'
        );
        $id = $this->db->insert('users', $data);
        $this->db->close();
    }

    function get_list_user($off = "", $limit = "") {
        $data = array();
        $this->db->select('*');
        $this->db->from('users');
        $this->db->limit($off, $limit);
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function num_rows() {
        $this->db->from('users');
        return $this->db->count_all_results();
    }

    function delete_user($id) {
        $this->db->where('id', $id);
        $this->db->delete('users');
    }

    function update_user($id, $acc, $name, $mail) {
        $sql = "UPDATE users SET id = '$id', acc = '$acc', email = '$email' ";
        $sql .= "name = '$name' ";
        $sql .= "email = '$email' ";
        $sql .= " WHERE id = $id;";
        $this->db->query($sql) or die(mysql_error());
        $this->db->close();
    }

    function edit_user($id) {
        $data = array(
//            'acc' => $acc,
//            'email' => $email,
//            'name' => $name
        );
//        $this->db->where('id', $id);
//        $this->db->update('users', $data);
        $sql = "SELECT * FROM users WHERE id = $id ";
        $result = $this->db->query($sql);
        foreach ($result->result_array() as $row) {
            $data[] = $row;
        }
        $result->free_result();
        $this->db->close();
        return $data;
        
    }

    public function getUserInfo($id) {
        $this->db->select('id, acc, email, name');
        $this->db->from('users');
        $this->db->where('id', $id);
        $rs = $this->db->get();
        if ($rs->num_rows == 0) {
            $rs->free_result();
            show_404();
        }
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
}

?>
